[XENAPI] Add shell option for xapi.py so it can remember sessions.
authorAlastair Tse <atse@xensource.com>
Fri, 20 Oct 2006 12:35:25 +0000 (13:35 +0100)
committerAlastair Tse <atse@xensource.com>
Fri, 20 Oct 2006 12:35:25 +0000 (13:35 +0100)
Signed-off-by: Alastair Tse <atse@xensource.com>
tools/python/scripts/xapi.domcfg.py
tools/python/scripts/xapi.py
tools/python/scripts/xapi.vbdcfg.py

index 6562383c19d4040bb99f6547d089e0e67a8eb1fc..70c009cbfb847a0f98e22af149b5ae3f07bcc7d7 100644 (file)
@@ -31,7 +31,7 @@ platform_enable_audio =  False
 builder =  ''
 boot_method =  '' # this will remove the kernel/initrd ??
 kernel_kernel =  '/boot/vmlinuz-2.6.16.29-xen'
-kernel_initrd =  '/root/initrd.img-2.6.16.29-xen.ramfs'
+kernel_initrd =  '/root/initrd-2.6.16.29-xen.img'
 kernel_args =  'root=/dev/sda1 ro'
 grub_cmdline =  ''
 PCI_bus =  ''
index 4b7b8d382bd63e79a42979086ec9b0b5ccc07d31..f44d37e13b62131dad68b83ea7f97e8e2a51a6e1 100644 (file)
@@ -26,7 +26,7 @@ MB = 1024 * 1024
 
 HOST_INFO_FORMAT = '%-20s: %-50s'
 VM_LIST_FORMAT = '%(name_label)-18s %(memory_actual)-5s %(vcpus_number)-5s'\
-                 ' %(power_state)-12s %(uuid)-36s'
+                 ' %(power_state)-10s %(uuid)-36s'
 SR_LIST_FORMAT = '%(name_label)-18s %(uuid)-36s %(physical_size)-10s' \
                  '%(type)-10s'
 VDI_LIST_FORMAT = '%(name_label)-18s %(uuid)-36s %(virtual_size)-8s '\
@@ -130,15 +130,19 @@ def execute(fn, *args):
         raise XenAPIError(*result['ErrorDescription'])
     return result['Value']
 
-
+_initialised = False
+_server = None
+_session = None
 def _connect(*args):
-    server = ServerProxy('httpu:///var/run/xend/xmlrpc.sock')
-    login = raw_input("Login: ")
-    password = getpass()
-    creds = (login, password)
-    session = execute(server.session.login_with_password, *creds)
-    host = execute(server.session.get_this_host, session)
-    return (server, session)
+    global _server, _session, _initialised
+    if not _initialised:
+        _server = ServerProxy('httpu:///var/run/xend/xmlrpc.sock')
+        login = raw_input("Login: ")
+        password = getpass()
+        creds = (login, password)
+        _session = execute(_server.session.login_with_password, *creds)
+        _initialised = True
+    return (_server, _session)
 
 def _stringify(adict):
     return dict([(k, str(v)) for k, v in adict.items()])
@@ -269,10 +273,11 @@ def xapi_vm_shutdown(*args):
     print 'Done.'
 
 def xapi_vbd_create(*args):
+    opts, args = parse_args('vbd-create', args)
+
     if len(args) < 2:
-        raise OptionError("Configuration file not specified")
+        raise OptionError("Configuration file and domain not specified")
 
-    opts, args = parse_args('vbd-create', args)
     domname = args[0]
     filename = args[1]
 
@@ -372,13 +377,67 @@ def xapi_vdi_rename(*args):
 #
 # Command Line Utils
 #
+import cmd
+class XenAPICmd(cmd.Cmd):
+    def __init__(self, server, session):
+        cmd.Cmd.__init__(self)
+        self.server = server
+        self.session = session
+        self.prompt = ">>> "
+
+    def default(self, line):
+        words = line.split()
+        if len(words) > 0:
+            cmd_name = words[0].replace('-', '_')
+            func_name = 'xapi_%s' % cmd_name
+            func = globals().get(func_name)
+            if func:
+                try:
+                    args = tuple(words[1:])
+                    func(*args)
+                    return True
+                except SystemExit:
+                    return False
+                except OptionError, e:
+                    print 'Error:', str(e)
+                    return False
+                except Exception, e:
+                    import traceback
+                    traceback.print_exc()
+                    return False
+        print '*** Unknown command: %s' % words[0]
+        return False
+
+    def do_EOF(self, line):
+        print
+        sys.exit(0)
+
+    def do_help(self, line):
+        usage(print_usage = False)
+
+    def postcmd(self, stop, line):
+        return False
 
-def usage(command = None):
+    def precmd(self, line):
+        words = line.split()
+        if len(words) > 0:
+            words0 = words[0].replace('-', '_')
+            return ' '.join([words0] + words[1:])
+        else:
+            return line
+
+def shell():
+    server, session = _connect()
+    x = XenAPICmd(server, session)
+    x.cmdloop('Xen API Prompt. Type "help" for a list of functions')
+
+def usage(command = None, print_usage = True):
     if not command:
-        print 'Usage: xapi <subcommand> [options] [args]'
-        print
-        print 'Subcommands:'
-        print
+        if print_usage:
+            print 'Usage: xapi <subcommand> [options] [args]'
+            print
+            print 'Subcommands:'
+            print
         sorted_commands = sorted(COMMANDS.keys())
         for command  in sorted_commands:
             args, description = COMMANDS[command]
@@ -394,10 +453,12 @@ def main(args):
         sys.exit(1)
 
     subcmd = args[0]
-
     subcmd_func_name = 'xapi_' + subcmd.replace('-', '_')
     subcmd_func = globals().get(subcmd_func_name, None)
-    if not subcmd_func or not callable(subcmd_func):
+
+    if subcmd == 'shell':
+        shell()
+    elif not subcmd_func or not callable(subcmd_func):
         print 'Error: Unable to find subcommand \'%s\'' % subcmd
         usage()
         sys.exit(1)
index faae8d407d9fa1e59fc0bea57be3929045b96ad9..82dcaf8ba7292c6b24401c1672d15292e00c4c78 100644 (file)
@@ -9,4 +9,4 @@ VDI =  ''
 device = 'sda1'
 mode = 'RW'
 driver = 'paravirtualised'
-image = 'file:/root/gentoo-64-xenU.img'
+image = 'file:/root/gentoo.amd64.img'